package com.admin_springboot.adminspringboot.controller;

import com.admin_springboot.adminspringboot.common.Constance;
import com.admin_springboot.adminspringboot.common.Result;
import com.admin_springboot.adminspringboot.entity.User;
import com.admin_springboot.adminspringboot.mapper.UserMapper;
import com.admin_springboot.adminspringboot.service.LoginService;
import com.admin_springboot.adminspringboot.service.UserService;
import com.admin_springboot.adminspringboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping()
public class LoginController {

    @Autowired
    private LoginService loginService;
    /**
     * 通过用户名或手机号码登录
     * @param user user对象
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        List<User> data = loginService.login(user);
        String token= TokenUtils.getToken(user.getUsername().toString(),user.getPassword());
        Map<String,Object> res = new HashMap();
        res.put("token",token);
        if (data.size()!= 0) {
            res.put("username",data.get(0).getUsername());
            return Result.success("登录成功",res);
        }
        return Result.error("用户名或密码错误");
    }
    @GetMapping("/menu")
    public Result LoadMenu() {
        List res=loginService.loadMenu();
        if(res.size()==0){
            return Result.error("菜单加载失败");
        }
        return Result.success("菜单加载成功",res);
    }
}
